package beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.naming.InitialContext;

import dao.DAO;
import dao.DAOFactory;
import ejb.VerifStock;

@ManagedBean
public class Ingredient {

	private int id;
	private String nom;
	private float prix;
	private int quantite;
	DAO<Ingredient> ingredientDAO = DAOFactory.getDAOIngredient();
	
	@EJB
	private VerifStock verifStock;
	
	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public float getPrix() {
		return prix;
	}
	public void setPrix(float prix) {
		this.prix = prix;
	}
	public int getQuantite() {
		return quantite;
	}
	public void setQuantite(int quantite) {
		this.quantite = quantite;
	}
	public int getId() {
		return id;
	}
	
	public Ingredient(int id, String nom, float prix, int quantite) {
		super();
		this.id = id;
		this.nom = nom;
		this.prix = prix;
		this.quantite = quantite;
	}
	public Ingredient(String nom, float prix, int quantite) {
		this(0, nom, prix, quantite);
	}
	public Ingredient(String nom, float prix) {
		this(0,nom,prix,0);
	}
	
	public void plus () {
		quantite ++;
		ingredientDAO.update(this);
	}
	
	public String moins () {
		try {
			InitialContext ic = new InitialContext();
			verifStock = (VerifStock) ic.lookup(VerifStock.class.getName());
			System.out.println("verifstock : "+verifStock.retraitOk(getId(), getQuantite())+" qte "+getQuantite());
			if(verifStock.retraitOk(getId(), getQuantite())) {
				quantite --;
				ingredientDAO.update(this);
			}
		} catch (Exception ex) {
			System.out.println("Impossible de contacter le bean Convertisseur.");
		}
		
		return null;
		
	}

}
